/*
 *  $Id: LocalizedTypeResolver.cs 1177 2010-11-08 11:38:40Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2010 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;

using Nereal.Serialization;

namespace Ragima.Languages {
    /// <summary>
    /// Определитель типов для локализованных ресурсов.
    /// </summary>
    public sealed class LocalizedTypeResolver : ITypeResolver {
        private const string AttributeName = "lang";

        #region ITypeResolver implementation
        public Type Deserialize(DeserializationContext context, Type baseType) {
            if (!context.HasAttribute(AttributeName))
                return baseType;
            var code = context.ReadAttribute(AttributeName);
            var lang = Language.AllLanguages.GetLanguage(code);
            return lang != null ? lang.GetLocalizedType(baseType) : baseType;
        }

        public string GetTypeElementName(Type objectType) {
            return null;
        }

        public void Serialize(SerializationContext context, Type baseType, Type objectType) {
            var lang = Language.AllLanguages.GetLanguage(objectType);
            if (lang != null)
                context.WriteAttribute(AttributeName, lang.LangCode);
        }
        #endregion
    }
}
